function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

var minute = 1000 * 60;
var hour = minute * 60;
var day = hour * 24;
var halfamonth = day * 15;
var month = day * 30;

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function formatTimeToHuman(millionsecends){
  var now = new Date().getTime()
  var diffValue = now - millionsecends
  if(diffValue < 0){
    return ''
  }
  var monthC = diffValue / month;
  var weekC = diffValue / (7 * day);
  var dayC = diffValue / day;
  var hourC = diffValue / hour;
  var minC = diffValue / minute;
  var result = ''

  if (hourC > 24) {
    var now = new Date(millionsecends)
    var year = now.getFullYear()
    var month = now.getMonth() + 1
    var day = now.getDate()
    result = parseInt(year) + "年" + parseInt(month) + "月" + parseInt(day)+"日";
  }
  else if (hourC >= 1&& hourC <=24) {
    result = parseInt(hourC) + "个小时前";
  }
  else if (minC >= 1&& minC <=60) {
    result = parseInt(minC) + "分钟前";
  } else {
    result = "刚刚";  
  }
  return result;
}

module.exports = {
  formatTime: formatTime,
  formatTimeToHuman: formatTimeToHuman
}
